package com.zretc.controller;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.zretc.entity.Orders;
import com.zretc.entity.OrdersDetail;
import com.zretc.service.BackOrdersService;
import com.zretc.util.Appliconfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Controller
public class FrontUserOrdersPayController {
    private static final Logger LOGGER = LoggerFactory.getLogger(FrontUserOrdersPayController.class);
    @Autowired
    private BackOrdersService backOrdersService;

    @RequestMapping("alipaySum")
    public Object alipayIumpSum(@ModelAttribute("ordersId") String ordersId,
                                HttpServletResponse response, HttpServletRequest request)
            throws Exception {
        // 获得订单
        Orders orders = backOrdersService.getOrders(ordersId);
        // 获取价格
        Float payPrice = orders.getPayPrice();
        // 获取商品名称（电影名称）
        String filmName = orders.getScreening().getFilm().getFilmName();
        // 获取商品描述(场次+影厅+座位)
        StringBuilder productDetails = new StringBuilder();
        productDetails.append("场次:")
                .append(orders.getScreening().getScreeningStartTime())
                .append("    影厅：")
                .append(orders.getScreening().getAuditoriums().getAuditoriumsName())
                .append("   座位：");
        for (OrdersDetail ordersDetail : orders.getOrdersDetails()) {
            productDetails.append(ordersDetail.getSeatLine()+"排"+ordersDetail.getSeatColumn()+"座  ");
        }
        // 获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(Appliconfig.gatewayUrl, Appliconfig.app_id,
                Appliconfig.merchant_private_key, "json", Appliconfig.charset,
                Appliconfig.alipay_public_key, Appliconfig.sign_type);
        // 设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(Appliconfig.return_url); // 设置同步界面
        alipayRequest.setNotifyUrl(Appliconfig.notify_url); // 设置异步界面
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + ordersId
                + "\"," + "\"total_amount\":\"" + payPrice
                + "\"," + "\"subject\":\"" + filmName + "\"," + "\"body\":\"" + productDetails.toString() + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        // 请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        // System.out.println(result);
        LOGGER.debug("==================================支付记录-开始记录========================================");
        LOGGER.debug(result);// 记录支付日志
        LOGGER.debug("==================================支付记录-结束记录========================================");
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println(result);
        return null;
    }
}
